package appletThree;


import globalCode.TableHandler;
import globalCode.TableHandlerContainer;
import globalCode.TableHandlerThread;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;




public class THTestApplet extends Applet implements TableHandlerContainer{
	TableHandler handler;

	public class InputPanel extends JPanel{
		public class TextFieldPanel extends JPanel{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			private JTextField idField;
			private JCheckBox updateBox;

			public TextFieldPanel(){

				this.setLayout(new GridLayout(2, 2));
				JLabel idFieldLabel = new JLabel();
				idFieldLabel.setText("Group ID");
				this.add(idFieldLabel);
				idField = new JTextField();
				this.add(idField);

				JLabel updateBoxLabel = new JLabel();
				updateBoxLabel.setText("Update Database");
				this.add(updateBoxLabel);
				updateBox = new JCheckBox();
				this.add(updateBox);

			}

			public String getInput() {
				String toReturn = new String();
				toReturn = idField.getText();
				return toReturn;
			}

			public boolean isChecked(){
				return updateBox.isSelected();
			}
		}
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		THTestApplet applet;
		private TextFieldPanel tFP;
		private JButton computeButton;

		public InputPanel(THTestApplet ma){
			this.setLayout(new BorderLayout());
			applet = ma;
			computeButton = new JButton();
			computeButton.addMouseListener(new MouseListener(){
				@Override
				public void mouseClicked(MouseEvent arg0) {
					applet.compute();
				}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {}
				@Override
				public void mouseReleased(MouseEvent arg0) {}
			});

			computeButton.setText("Compute");
			computeButton.setPreferredSize(new Dimension(50, 20));
			this.add(computeButton, BorderLayout.SOUTH);

			tFP = new TextFieldPanel();
			tFP.setPreferredSize(new Dimension(350, 75));
			this.add(tFP, BorderLayout.NORTH);
		}

		public String getInput() {
			return tFP.getInput();
		}

		public boolean isChecked(){
			return tFP.isChecked();
		}

		public void setButtonText(String string) {
			computeButton.setText(string);
		}
	}
	public class OutputPanel extends JPanel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private JScrollPane outputPane;
		private JTable outputTable;
		private Object[] labels = new Object[]{"User 1", "User 2", "Score"};
		private Object[][] data = new Object[][]{{1, 1, 1},{2,2,2},{3,3,3}};

		public OutputPanel(){
			this.setLayout(new BorderLayout());
			JLabel outputLabel = new JLabel();
			outputLabel.setText("Output");
			outputLabel.setPreferredSize(new Dimension(375, 15));
			this.add(outputLabel, BorderLayout.NORTH);

			outputTable = new JTable(data, labels);
			outputPane = new JScrollPane(outputTable);
			//outputArea.setEnabled(false);
			outputPane.setPreferredSize(new Dimension(375, 275));
			this.add(outputPane, BorderLayout.SOUTH);
		}

		public void print(String message) {
			//outputArea.setText(message);
			repaint();
		}

		public void display(int[][] rankings) {
			Integer[][] inteRankings = new Integer[rankings.length][];
			for(int i = 0; i<rankings.length; i++){
				inteRankings[i] = new Integer[rankings[i].length];
				int j = 0;
				//System.out.println("ROW: "+i);
				for(int r: rankings[i]){
					inteRankings[i][j++] = r;
					//System.out.println(r);
				}
				//System.out.println();
			}
			data = inteRankings;
			this.remove(outputPane);
			outputTable = new JTable(data, labels);
			outputPane = new JScrollPane(outputTable);
			outputPane.setPreferredSize(new Dimension(350, 275));
			this.add(outputPane);
			validate();
		}
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	InputPanel input;
	OutputPanel output;
	private JProgressBar progress;

	boolean otherRunning = false;
	public void compute() {
		System.out.println("COMPUTE ONCE");
		if(otherRunning){
			handler.endAll();
			otherRunning = false;
			progress.setValue(0);
			input.setButtonText("Compute");
			return;
		}
		String inputStr = input.getInput();
		int group = -1;
		try{
			group = Integer.parseInt(inputStr);
		}catch(NumberFormatException ex){
			output.print("Invalid Input");
			return;
		}




		if(input.isChecked()){
			handler.loadThreaded(this, group);
			otherRunning = true;
			//other.setDaemon(true);
			input.setButtonText("Cancel");
			//output.display(gcs.getRankings());
		}
		else{
			thtDisplay(handler.getShortArray(group));
		}
	}

	@Override
	public void start(){
		this.setSize(610, 460);
		this.setVisible(true);
		Border b1 = BorderFactory.createLineBorder( Color.GRAY );
		JPanel child = new JPanel();
		child.setPreferredSize(new Dimension(600, 450));
		child.setLayout(new BorderLayout());
		input = new InputPanel(this);
		child.add(input, BorderLayout.NORTH);
		output = new OutputPanel();
		child.add(output, BorderLayout.CENTER);
		progress = new JProgressBar();
		child.add(progress, BorderLayout.SOUTH);
		this.add(child);
		child.setBorder(b1);

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			output.print(e.getMessage());
			return;
		}

		String url = "jdbc:mysql://ischooldev.fcps.net";
		//String url = "jdbc:mysql://dosshome.dyndns-office.com:63306";
		//String url = "jdbc:mysql://192.168.1.33:3306";
		String username = "root";
		String password = "PAss33word!!";
		Connection con = null;

		try {
			con = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			output.print(e.getMessage());
			return;
		}

		handler = new TableHandler(con);
	}

	@Override
	public void stop(){
		System.exit(0);
	}
	//Only called by a GroupConnectionsThread
	public void thtDisplay(int[][] rankings){
		output.display(rankings);
		otherRunning = false;
		input.setButtonText("Compute");
	}

	@Override
	public void thtUserFinished() {
		progress.setValue(progress.getValue()+1);
		
	}

	@Override
	public void thtGroupFinished(TableHandlerThread tht) {
		progress.setValue(progress.getMaximum());
		handler.removeThread(tht);
	}

	@Override
	public void thtUsersInGroup(int num) {
		progress.setMaximum(num);
		progress.setValue(0);
	}

	@Override
	public void thtGroupsInSet(int num) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void thtAbortedGroupThread() {
		progress.setValue(0);
	}

	@Override
	public void thtAbortedRebuild() {
		// TODO Auto-generated method stub
		
	}


}
